/**
 * @FunctionUtils.as
 *
 * @author sodaChen mail:asframe#163.com
 * @version 1.0
 * <br>Copyright (C), 2012 ASFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame
 * <br>Date:2012-2-1
 */
package com.asframe.utils
{
	import flash.display.Sprite;

	/**
	 *
	 * @author sodaChen
	 * Date:2012-2-1
	 */
	public class FunctionUtils
	{
		public static function executeFun(fun:Function,param:Object = null):void
		{
//			try
//			{
				if(fun.length == 0)
					fun();
				else
					fun(param);
//			}
//			catch(error:Error)
//			{
//				trace("执行executeFun报错:" + error.getStackTrace());
//			}

		}
		public static function callFuncTarget(data:*,func:Function,target:*):void
		{
			if(func.length == 2)
			{
//				try
//				{
					func(data,target);
//				}
//				catch(error:Error)
//				{
//					throw new Error(getFuncName(func) + "出错：" +　error.getStackTrace());
//				}
			}
			else
			{
				try
				{
					func(data);
				}
				catch(error1:Error)
				{
					throw new Error(getFuncName(func) + "出错：" +　error1.getStackTrace());
				}
			}
		}
		public static function executeParam(fun:Function,param:Object = null):void
		{
			if(fun == null)
			{
				return ;
			}
			if(param == null)
			{
				execute(fun);
			}
			else
			{
				execute(fun,[param]);
			}
		}
		/**
		 * 获取函数的名称
		 * @param fun
		 * @return
		 *
		 */
		public static function getFuncName(fun:Function):String
		{
			try
			{
				Sprite(fun);
			}
			catch(err:Error)
			{
				var fn:String = err.message.replace(/.+::(\w+\/\w+)\(\)\}\@.+/,"$1");
				return fn==err.message?(err.message.replace(/.+ (function\-\d+) .+/i,"$1")):fn;
			}
			return null;
		}
		public static function execute(fun:Function,params:Array = null,errorFun:Function = null):void
		{
			if(fun != null)
			{
//				try
//				{
					fun.apply(null,params);
//				}
//				catch(error:Error)
//				{
//					trace(getFuncName(fun) + "出错：" +　error.getStackTrace());
//					if(errorFun != null)
//					{
//						errorFun(error);
//					}
//				}
			}
		}
	}
}
